package jdbc;

import com.mysql.cj.x.protobuf.MysqlxExpect;

import java.sql.*;

public class DBUtil {
    private DBUtil(){

    }
    //静态会在类加载中运行
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection connection() throws SQLException {
        Connection conn = null;

        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_user", "root", "314159");
        return conn;
    }

    public static void close(Connection conn, PreparedStatement ps, ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
